#include <iostream>
#include <vector>
using namespace std;

int res = 0;
int t = 0;

void dfs(vector<vector<int>>& tree, int u,int& prev)
{
    if (tree[u][0] != 0)
        dfs(tree, tree[u][0], prev);
    if (res == 0 && prev == t)
    {
        res = u;
        return;
    }
    prev = u;
    if (tree[u][1] != 0)
        dfs(tree, tree[u][1], prev);
}

int main()
{
    int n, root;
    cin >> n >> root;
    vector<vector<int>> tree(n + 1, vector<int>(2));
    int a, b, c;
    for (int i = 0; i < n; i++)
    {
        cin >> a >> b >> c;
        tree[a] = { b,c };
    }
    cin >> t;

    int prev = 0;
    dfs(tree, root, prev);

    cout << res << endl;
    return 0;
}